package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gareth Cao
 * @date 2022/4/30 9:26
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    //一旦新增套餐，就立刻删除所有key
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> add(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增菜品成功");
    }

    /**
     * 分页条件查询套餐
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByDesc("update_time");
        setmealService.page(pageInfo, queryWrapper);

        //页面需要的响应数据应该是setmealDto封装的
        Page<SetmealDto> pageInfoDto = new Page<>();
        //2.1 将pageInfo对象中除了records属性之外的属性拷贝到pageInfoDto中
        BeanUtils.copyProperties(pageInfo, pageInfoDto, "records");
        List<Setmeal> setmealList = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            //2.2将集合中属性拷贝到setmealDto中
            BeanUtils.copyProperties(setmeal, setmealDto);
            //setmealDto属性还需要categoryName
            Category category = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            //每一个setmealDto存到集合中
            setmealDtoList.add(setmealDto);
        }
        //将List集合存到records属性中
        pageInfoDto.setRecords(setmealDtoList);
        return R.success(pageInfoDto);
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

    /**
     * 手机端页面展示套餐信息
     */
    @GetMapping("/list")
    //查询结果为空就不往缓存中添加数据
    @Cacheable(cacheNames = "setmeal", key = "#categoryId",unless = "#result.data.size()==0")
    public R<List<Setmeal>> selectByCategoryIdAndStatus(Long categoryId, Integer status) {
        //根据条件查询展示到手机端页面
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("status", status);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }

    /**
     * 通过点击套餐图片展示信息
     * @param id
     */
    @GetMapping("/dish/{id}")
    public R<Setmeal> selectById(Long id) {
        //根据参数套餐id 条件查询展示到手机端页面
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        Setmeal setmeal = setmealService.getById(id);
        return R.success(setmeal);
    }
}
